#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import time
from multiprocessing.dummy import Pool
import requests
#同步代码
# urls = [
#     'http://127.0.0.1:5000/bobo',
#     'http://127.0.0.1:5000/jay',
#     'http://127.0.0.1:5000/tom'
# ]
# def get_request(url):
#     page_text = requests.get(url).text
#     print(len(page_text))
#
# if __name__ == "__main__":
#     start = time.time()
#     for url in urls:
#         get_request(url)
#     print('总耗时：',time.time()-start)


#基于线程池的异步效果
urls = [
    'http://127.0.0.1:5000/bobo',
    'http://127.0.0.1:5000/jay',
    'http://127.0.0.1:5000/tom'
]
def get_request(url):
    page_text = requests.get(url).text
    return len(page_text)

if __name__ == "__main__":
    start = time.time()

    pool = Pool(3) #启动了三个线程
    #参数1：回调函数
    #参数2：可迭代的对象，alist
    #作用：可以将alist中的每一个元素依次传递给回调函数作为参数，然后回调函数会异步
        #对列表中的元素进行相关操作运算
    #map的返回值就是回调函数返回的所有结果
    page_text_len_list = pool.map(get_request,urls)
    print(page_text_len_list)


    print('总耗时：',time.time()-start)